<?php
defined('IN_IA') or exit('Access Denied');
global $_GPC, $_W;
//系统配置

$config=$this->config();
$member=m('member')->get_uidopenid();


$op=!empty($_GPC['op'])?$_GPC['op']:'index';
$fcategory=!empty($_GPC['fcategory'])?$_GPC['fcategory']:$config['wz1'];

if ($op=='index') {
    $cid=$_GPC['id'];
    $type=$_GPC['type'];
    $order=$_GPC['order'];
    $keywords=$_GPC['keywords'];

    $where=" uniacid=:uniacid and fcategory=:fcategory";
    $array=[':uniacid'=>$_W['uniacid'],':fcategory'=>$fcategory];
    if($cid){
        $where.=" and category=:cid ";
        $array[':cid']=$cid;
    }

    if($type=="all"){

    }elseif($type=="free"){
        $where.=" and free=0 ";

    }elseif($type=="vip"){
        $where.=" and free=1 ";
    }
    if(!empty($keywords)){

        $where.=" and name LIKE '%".$keywords."%'";

    }
    if($order=="xl"){

        $order=" order by view desc ";
    }else{
        $order=" order by id desc ";

    }

    $sql='select * from '.tablename('xuan_hb_poster_data')." where 1 and {$where} {$order} limit 18";
    $list=pdo_fetchall($sql,$array);

    $categorylist=m('category')->getList(array('parentid'=>$fcategory,'enabled'=>1,'order'=>' displayorder asc'));

    $tou=pdo_fetchall("SELECT url,logo FROM ".tablename('xuan_hb_huan').' where status=1 and type=:type and uniacid=:uniacid order by rank desc',array(':type'=>'首页',':uniacid'=>$_W['uniacid']));

    include $this->template('index'); 

	
}elseif($op=='upload'){
    $flag=$_GPC['flag'];
    $file = $_FILES['fileName'.$flag];//得到传输的数据
    
    //得到文件名称
    $name = $file['name'];
    $type = strtolower(substr($name,strrpos($name,'.')+1)); //得到文件类型，并且都转化成小写
    $allow_type = array('jpg','jpeg','gif','png'); //定义允许上传的类型
    //判断文件类型是否被允许上传
    if(!in_array($type, $allow_type)){
      //如果不被允许，则直接停止程序运行
      return ;
    }
    //判断是否是通过HTTP POST上传的
    if(!is_uploaded_file($file['tmp_name'])){
      //如果不是通过HTTP POST上传的
      return ;
    }
	$upload_path = "../addons/xuan_zy/public/user/"; //上传文件的存放路径
	$url=$upload_path.md5($file['name'].time()).'.'.$type;
	//开始移动文件到相应的文件夹
    if(move_uploaded_file($file['tmp_name'],$url)){
	  
		include $this->template('upload');
    }else{
      echo "Failed!";
    }

}elseif($op=='upload2'){
    $avatar=$_GPC['avatar'];

    $acc = WeAccount::create($_W['acid']);
    $file=$acc->downloadMedia(array('type'=>'image','media_id'=>$avatar));
    echo json_encode(['file'=>$file]);
}elseif($op=='detail'){
    $id=$_GPC['id']?$_GPC['id']:0;

    $tou=pdo_fetch("SELECT url,logo FROM ".tablename('xuan_hb_huan').' where status=1 and type=:type and uniacid=:uniacid order by rank desc',array(':type'=>'生成详情页',':uniacid'=>$_W['uniacid']));

    if($id){

        $info = pdo_fetch("SELECT view,ext_info FROM ".tablename('xuan_hb_poster_data').' WHERE id=:id', array(':id'=>$id));
        $ext_info = json_decode($info['ext_info'], 1);

        $collect=pdo_get('xuan_hb_collect',['id'=>$id,'uid'=>$member['uid'],'uniacid'=>$_W['uniacid']]);
        $collect=$collect?'1':'0';
        /*检测图片是否存在*/
        if(file_exists("../addons/xuan_zyy/public/img/{$member['uid']}-{$id}.jpg")){

        }else{
            $value =$this->createMobileUrl('index');

            $params=array();
            $params['poster_id']=$id;

            $params['url']=$value;
            $params['member']['avatar']=$member['avatar'];
            $params['member']['phone']='18831111111';
            $params['member']['title']='自定义内容';
            $params['member']['nickname']=$member['nickname'];

            $params['out']="../addons/xuan_zyy/public/img/{$member['uid']}-{$id}.jpg";

            $result=m('poster')->createPoster($params);

        }
        $params['out']="../addons/xuan_zyy/public/img/{$member['uid']}-{$id}.jpg?t=".time();

        pdo_update('xuan_hb_poster_data',['view'=>$info['view']+1],['id'=>$id]);
    }
    include $this->template('detail');
}elseif($op=='collect'){
    header('Content-type: application/json');
    $id=$_GPC['id'];
    if($id){

        if($_GPC['type']==0){

            pdo_insert('xuan_hb_collect',['uid'=>$member['uid'],'	cid'=>$id,'createtime'=>date('Y-m-d H:i:s',time()),'uniacid'=>$_W['uniacid']]);
            echo json_encode(['status'=>1]);
        }elseif($_GPC['type']==1){

            pdo_delete('xuan_hb_collect',['uid'=>$member['uid'],'	cid'=>$id,'uniacid'=>$_W['uniacid']]);
            echo json_encode(['status'=>2]);
        }



    }

}elseif($op=='ajaxsc'){
    header('Content-type: application/json');
    $id=$_GPC['id'];
    $avatar=$_GPC['avatar'];
    $phone=$_GPC['phone'];
    $title=$_GPC['title'];
    $nickname=$_GPC['name'];
    $url=$_GPC['url'];
    if($id){
        $params=array();
        $params['poster_id']=$id;

        $params['url']=$url;
        $params['member']['avatar']=$avatar;
        $params['member']['phone']=$phone;
        $params['member']['title']=$title;
        $params['member']['nickname']=$nickname;

        $params['out']="../addons/xuan_zyy/public/img/{$member['uid']}-{$id}.jpg";

        $result=m('poster')->createPoster($params);
    }
    echo json_encode(['status'=>1,'url'=>$params['out'].'?t_='.time()]);

}elseif($op=='ajaxlist'){
    $fcategory=!empty($_GPC['fcategory'])?$_GPC['fcategory']:$config['wz1'];
    $page=isset($_GPC['page'])?intval($_GPC['page']):1; ;
    $num="18";
    $start=($page-1)*$num;
    $limit = "limit ".$start.",".$num;

    $cid=$_GPC['id'];
    $type=$_GPC['type'];
    $order=$_GPC['order'];
    $keywords=$_GPC['keywords'];

    $where=" uniacid=:uniacid and fcategory=:fcategory";
    $array=[':uniacid'=>$_W['uniacid'],':fcategory'=>$fcategory];

    if($cid){
        $where.=" and category=:cid ";
        $array[':cid']=$cid;
    }
    if($type=="all"){

    }elseif($type=="free"){
        $where.=" and free=0 ";

    }elseif($type=="vip"){
        $where.=" and free=1 ";
    }

    if($order=="xl"){

        $order=" order by view desc ";
    }else{
        $order=" order by id desc ";

    }
    if(!empty($keywords)){

        $wheres.=" and name LIKE '%".$keywords."%'";

    }
    $order=" order by id desc ";
    $sql='select * from '.tablename('xuan_hb_poster_data')." where 1 and {$where} {$order} {$limit}";
    $list=pdo_fetchall($sql,$array);
    echo json_encode($list);
}

	
 
